import path from 'path'
import webpack from 'webpack'
import precss from 'precss'
import autoprefixer from 'autoprefixer'
import _debug from 'debug'

import config from '../../config'


const debug = _debug('app:webpack-config')
const paths = config.utils_paths
const {__DEV__, __PROD__, __TEST__} = config.globals


debug('Create configuration.')



const APP_ENTRY_PATH = paths.client('client.js')   //get the absolute path

// const webpack_plugins = [
//     new webpack.optimize.OccurenceOrderPlugin(),
//     // Pass in config variables.
//     new webpack.DefinePlugin(config.globals),
// ]

// if (__DEV__) {
//   debug('Enable plugins for live development (HMR, NoErrors).')
//   webpack_plugins.push(
//     new webpack.HotModuleReplacementPlugin(),
//     // new webpack.NoErrorsPlugin()
//   )
// } 

// const webpack_entry = [
//     __DEV__
//     ? [APP_ENTRY_PATH, 'webpack-hot-middleware/client']
//     : [APP_ENTRY_PATH]
// ]



module.exports = {
  // Can cause warnings in the latest Chrome
  // devtool: 'cheap-module-eval-source-map',
  
  devtool: 'cheap-source-map',
  entry: ['webpack-hot-middleware/client',APP_ENTRY_PATH],
  output: {
    filename: 'bundle.js',
    chunkFilename: '[name].chunk.js',
    path: paths.dist(),
    publicPath: config.compiler_public_path
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),

    // Pass in config variables.
    new webpack.DefinePlugin(config.globals),
    new webpack.optimize.CommonsChunkPlugin('shared.js')
  ],
  module: {
    loaders: [
      {
        test: /\.(js|jsx)$/,
        loaders: [ 'babel' ],
        exclude: /node_modules/,
        // include: __dirname,
        // query: {
        //   cacheDirectory: true,
        //   // plugins: [['transform-runtime'],["transform-object-rest-spread"],["syntax-decorators"]],
        //   plugins: ['transform-runtime'],
        //   presets: ['es2015', 'react', 'stage-0'],
        //   env: {
        //     development: {
        //       presets: ['react-hmre']
        //     }
        //   }
        // }
      },
      { test: /\.css$/, loader: 'style-loader!css-loader!'},
      { test: /\.less$/, loader: 'style!css?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]!postcss-loader!less?outputStyle=expanded&sourceMap' },
      { test: /\.scss$/, loader: 'style!css?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]!postcss-loader!sass?outputStyle=expanded&sourceMap' },
      { test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
      { test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
      { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" },
      { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file" },
      { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml" },
    ]
  },
  postcss: function () {
    return [precss, autoprefixer];
  }
}
